package application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class ConnectionDanMu {
	private String request_rul="";
	private String request_roomid="";//房间号
	private HttpURLConnection conn;//链接
	
	public ConnectionDanMu() {
		
	}

	public String getRequest_rul() {
		return request_rul;
	}

	public void setRequest_rul(String request_rul) {
		this.request_rul = request_rul;
	}

	public String getRequest_roomid() {
		return request_roomid;
	}

	public void setRequest_roomid(String request_roomid) {
		this.request_roomid = request_roomid;
	}

	public boolean connection() {
		try {
			URL url=new URL(request_rul);
			conn=(HttpURLConnection)url.openConnection();
			//提交
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);//不使用缓存
			
			//设置请求的属性 提交表单
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			//链接
			conn.connect();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	/**
	 * 提交数据
	 * @return  返回json数据
	 */
	public  String getDanMuData() {
		try {
			OutputStreamWriter out=new OutputStreamWriter(conn.getOutputStream());
			
			String post_value1="roomid="+URLEncoder.encode(request_roomid,"UTF-8");
			String post_value2="&csrf_token="+URLEncoder.encode("d109486c9e7e4c23bf5f0afc9caacec7","UTF-8");
			String post_value3="&csrf="+URLEncoder.encode("d109486c9e7e4c23bf5f0afc9caacec7","UTF-8");
			String post_value4="&visit_id="+URLEncoder.encode("","UTF-8");
			//拼接
			String post_value=post_value1+post_value2+post_value3+post_value4;
			out.write(post_value);
			//刷新
			out.flush();
			out.close();
			
			BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			String rl="";
			String data="";
			while ((rl=reader.readLine())!=null) {
				data=data+rl;
			}
			reader.close();
			return data;
			
		} catch (Exception e) {
			System.out.println("获取数据错误");
			return null;
		}		
		
	}
	public static ObservableList<DanmuData> findData(String data){
		String str_value=data.replaceAll("vip","\r\n");
		//System.out.println(str_value);
		String pat="text(.*)isadmin";
		//规则
		Pattern pattern=Pattern.compile(pat);
		//匹配的数据
		Matcher matcher=pattern.matcher(str_value);
		
		ObservableList<DanmuData> list=FXCollections.observableArrayList();
		while (matcher.find()) {
			DanmuData damd=new DanmuData();
			
			String temp_data=matcher.group();
			String patt_text="text(.*)nickname";
			Pattern pattern_text=Pattern.compile(patt_text);
			Matcher matcher_text=pattern_text.matcher(temp_data);
			//抓取弹幕
			while (matcher_text.find()) {
				String temp_str=matcher_text.group();
				String text=temp_str.substring(7,temp_str.length()-11);
				damd.setText(text);
			}
			//-----------------------
			
			String patt_nickname="nickname(.*)uname_color";
			Pattern pattern_nickname=Pattern.compile(patt_nickname);
			Matcher matcher_nickname=pattern_nickname.matcher(temp_data);
			//抓取昵称
			while (matcher_nickname.find()) {
				String temp_nickname=matcher_nickname.group();
				String nickname=temp_nickname.substring(11,temp_nickname.length()-14);
				damd.setNickename(nickname);
			}
		    
			
			//_----------------------
			String patt_timeline="timeline(.*)isadmin";
			Pattern pattern_timeline=Pattern.compile(patt_timeline);
			Matcher matcher_timeline=pattern_timeline.matcher(temp_data);
			//抓取昵称
			while (matcher_timeline.find()) {
				String temp_timeline=matcher_timeline.group();
				String timeline=temp_timeline.substring(11,temp_timeline.length()-10);
				damd.setTimeline(timeline);
			}
			list.add(damd);
		}
		return list;
	}
	public void disconnect() {
		if(conn!=null) {
			conn.disconnect();
		}
	}
}
